package org.linlinjava.litemall.admin.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.SysDict;
import org.linlinjava.litemall.db.domain.SysDictItem;
import org.linlinjava.litemall.db.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dicts")
@Validated
public class AdminDictController {

    @Autowired
    private SysDictService dictService;

    @GetMapping("")
    public Object list(SysDict dict) {
        List<SysDict> dictList = dictService.listDict(dict);
        return ResponseUtil.ok(dictList);
    }

    @PostMapping("")
    @RequiresPermissions("admin:dict:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "字典"}, button = "添加")
    public Object create(@RequestBody SysDict dict) {
        if(dictService.createDict(dict)) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail();
        }
    }

    @GetMapping("/{dictId}")
    public Object get(@PathVariable Integer dictId) {
        SysDict dict = dictService.getDictById(dictId);
        return ResponseUtil.ok(dict);
    }

    @PutMapping("/{dictId}")
    @RequiresPermissions("admin:dict:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "字典"}, button = "更新")
    public Object update(@PathVariable Integer dictId, @RequestBody SysDict dict) {
        if(dictService.updateDict(dict)) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail();
        }
    }
    @DeleteMapping("/{dictId}")
    @RequiresPermissions("admin:dict:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "字典"}, button = "删除")
    public Object delete(@PathVariable Integer dictId) {
        if(dictService.deleteDict(dictId)) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail();
        }
    }

    @GetMapping("/{dictId}/items")
    public Object listDictItems(@PathVariable Integer dictId) {
        List<SysDictItem> dictItemList = dictService.listDictItems(dictId);
        return ResponseUtil.ok(dictItemList);
    }

    @GetMapping("/items")
    public Object listItems(String dictName, @RequestParam(required = false) String dictCode) {
        List<SysDictItem> dictItemList = null;
        if (StringUtils.isNotEmpty(dictName)) {
            dictItemList = dictService.listItemsByDictName(dictName);
        } else if (StringUtils.isNotEmpty(dictCode)) {
            dictItemList = dictService.listItemsByDictCode(dictCode);
        }
        return ResponseUtil.ok(dictItemList);
    }

    @PostMapping("/{dictId}/items")
    @RequiresPermissions("admin:dictitem:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "字典"}, button = "添加字典项")
    public Object createDictItem(@PathVariable("dictId") int dictId, @RequestBody SysDictItem item) {
        if(null == dictService.getDictById(dictId)) {
            throw new RuntimeException("找不到id为" + dictId + "的字典");
        }
        if(dictService.createDictItem(item)) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail();
        }
    }

    @PutMapping("/{dictId}/items")
    @RequiresPermissions("admin:dictitem:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "字典"}, button = "更新字典项")
    public Object upadteDictItem(@PathVariable("dictId") int dictId, @RequestBody SysDictItem item) {
        if(null == dictService.getDictById(dictId)) {
            throw new RuntimeException("找不到id为" + dictId + "的字典");
        }
        if(dictService.updateDictItem(item)) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail();
        }
    }

    @DeleteMapping("/{dictId}/items/{dictItemId}")
    @RequiresPermissions("admin:dictitem:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "字典"}, button = "删除字典项")
    public Object deleteDictItem(@PathVariable("dictId") int dictId, @PathVariable("dictItemId") int dictItemId) {
        if(null == dictService.getDictById(dictId)) {
            throw new RuntimeException("找不到id为" + dictId + "的字典");
        }
        if(null == dictService.getDictItemById(dictItemId)) {
            throw new RuntimeException("找不到id为" + dictItemId + "的字典项");
        }
        if(dictService.deleteDictItem(dictItemId)) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail();
        }
    }

}
